package com.oisix.oisystemts2.service.hassou;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oisix.oisystemec.ECNames;
import com.oisix.oisystemec.model.Juchuu;
import com.oisix.oisystemec.model.Juchuumeisai;
import com.oisix.oisystemec.model.Shouhin;
import com.oisix.oisystemec.model.Shouhinbunrui;
import com.oisix.oisystemec.model.Shouhinshoukai;
import com.oisix.oisystemec.model.Shouhinshurui;
import com.oisix.oisystemfr.util.StringUtil;

/**
 * Process and get value of fields to hassou2.csv.
 * @author TungTDH
 * @version 2010/12/02
 */
public class Hassou2Csv {

    /**
     * Field of Hassou2.csv file.
     */
    public enum Fields {
        /** 受注番号. */
        JUCHUU_ID,
        /** 行番号. */
        INDEX,
        /** 商品名. */
        SHOUHINMEI,
        /** 基準. */
        KIKAKU,
        /** 売価. */
        BAIKA,
        /** 商品数量. */
        SUURYOU,
        /** 総計. */
        SHOUKEI,
        /** 栽培形態. */
        SAIBAIKEITAI,
        /** 目安. */
        MEYASU,
        /** 才数. */
        SAISUU,
        /** 表示順序. */
        HYOUJIJUNJO,
        /** 商品分類名. */
        SHOUHINBUNRUI,
        /** 商品種類名. */
        SHOUHINSHURUI,
        /** 商品番号. */
        SHOUHIN_ID,
        /** 商品バージョン. */
        SHOUHIN_VER,
        /** 冷凍フラグ. */
        REITOU_FLG,
        /** クール表示. */
        COOLHYOUJI,
        /** 空白. */
        BLANK,
        /** 定価. */
        TEIKA,
        /** おいくら限定フラグ. */
        OIKURAGENTEI_FLG,
        /** 加工品区分. */
        KAKOUHINKUBUN
    }

    /** Total count field of hassou csv file. */
    private static final int FIELD_HASSOU2CSV_COUNT = 21;

    /** Juchuumeisai info list. */
    private List<String[]> dataList;

    /** Juchuu. */
    private Juchuu juchuu;

    public List<String[]> getDataList() {
        return dataList;
    }

    public void setValuesList(List<String[]> dataList) {
        this.dataList = dataList;
    }

    /**
     * Contructors.
     */
    public Hassou2Csv() {

    }

    /**
     * Set values for hassou2.csv file.
     * @param juchuu
     * @return this.dataList
     */
    public List<String[]> createDataList(Juchuu juchuu) {
        this.juchuu = juchuu;
        this.dataList = new ArrayList<String[]>();
        int count = juchuu.getJuchuumeisais().size();
        for (int i = 0; i < count; i++) {
            Juchuumeisai juchuumeisai = juchuu.getJuchuumeisais().get(i);
            if (juchuumeisai.getShouhin() != null) {
                String[] csvInfo = generateCsvData(juchuumeisai, i + 1);
                this.dataList.add(csvInfo);
            }
        }
        return this.dataList;
    }

    /**
     * Generate csv info string array.
     * @param juchuumeisai
     * @param index
     * @return CsvInfo
     */
    private String[] generateCsvData(Juchuumeisai juchuumeisai, int index) {
        String [] data = new String[FIELD_HASSOU2CSV_COUNT];
        Shouhin shouhin = juchuumeisai.getShouhin();
        data[Fields.JUCHUU_ID.ordinal()] = this.juchuu.getJuchuuId();
        data[Fields.INDEX.ordinal()] = Integer.toString(index);
        data[Fields.SHOUHINMEI.ordinal()] = this.getShouhinmei(juchuumeisai);
        data[Fields.KIKAKU.ordinal()] = juchuumeisai.getShouhin().getKikaku();
        data[Fields.BAIKA.ordinal()] = this.getBaika(juchuumeisai);
        data[Fields.SUURYOU.ordinal()] = StringUtil.nullToKara(juchuumeisai
                .getSuuryou());
        data[Fields.SHOUKEI.ordinal()] = this.getShoukei(juchuumeisai);
        data[Fields.SAIBAIKEITAI.ordinal()] = this.getSaibaikeitai(shouhin);
        data[Fields.MEYASU.ordinal()] = shouhin.getMeyasu();
        data[Fields.SAISUU.ordinal()] = StringUtil.nullToKara(shouhin
                .getSaisuu());
        data[Fields.HYOUJIJUNJO.ordinal()]
             = StringUtil.nullToKara(shouhin.getInd());
        data[Fields.SHOUHINBUNRUI.ordinal()] = this.getShouhinbunrui(shouhin);
        data[Fields.SHOUHINSHURUI.ordinal()] = getShouhinshurui(shouhin);
        data[Fields.SHOUHIN_ID.ordinal()] = juchuumeisai.getShouhinId();
        data[Fields.SHOUHIN_VER.ordinal()]
             = juchuumeisai.getShouhinVer().trim();
        data[Fields.REITOU_FLG.ordinal()] = this.getReitouFlg(shouhin);
        data[Fields.COOLHYOUJI.ordinal()] = this.getCoolhyouji(shouhin);
        data[Fields.BLANK.ordinal()] = StringUtils.EMPTY;
        data[Fields.TEIKA.ordinal()] = this.getTeika(juchuumeisai);
        data[Fields.OIKURAGENTEI_FLG.ordinal()] = this
                .getOikuragenteiFlg(shouhin);
        data[Fields.KAKOUHINKUBUN.ordinal()] = StringUtil.nullToKara(shouhin
                .getKakouhinkubun());
        return data;
    }

    /**
     * Get value for fied shouhinmei.
     *
     * @param juchuumeisai
     * @return Shouhinmei
     */
    private String getShouhinmei(Juchuumeisai juchuumeisai) {
        if (juchuumeisai.getCommentFlg().intValue() == 1) {
            return "　(" + juchuumeisai.getShouhin().getShouhin() + ")";
        }
        return juchuumeisai.getShouhin().getShouhin();
    }

    /**
     * Get value for fied baika.
     * @param juchuumeisai
     * @return baika
     */
    private String getBaika(Juchuumeisai juchuumeisai) {
        if (juchuumeisai.getCommentFlg().intValue() == 1) {
            return StringUtils.EMPTY;
        } else if (juchuu.getShouhizeikubun() == ECNames.TAX_ZEIKOMI) {
            return juchuumeisai.getZeikomiBaika().setScale(0,
                    BigDecimal.ROUND_HALF_UP).toString();
        } else {
            return juchuumeisai.getBaika()
                    .setScale(0, BigDecimal.ROUND_HALF_UP).toString();
        }
    }

    /**
     * Get value for fied shoukei.
     *
     * @param juchuumeisai
     * @return shoukei
     */
    private String getShoukei(Juchuumeisai juchuumeisai) {
        if (juchuumeisai.getCommentFlg().intValue() == 1) {
            return StringUtils.EMPTY;
        } else if (juchuu.getShouhizeikubun() == ECNames.TAX_ZEIKOMI) {
            return Integer
                    .toString(juchuumeisai.getZeikomiBaika().setScale(0,
                            BigDecimal.ROUND_HALF_UP).intValue()
                            * juchuumeisai.getSuuryou().intValue());
        } else {
            return Integer.toString(juchuumeisai.getBaika().intValue()
                    * juchuumeisai.getSuuryou().intValue());
        }
    }

    /**
     * Get value for fied teika.
     *
     * @param juchuumeisai
     * @return teika
     */
    private String getTeika(Juchuumeisai juchuumeisai) {
        if (juchuumeisai.getCommentFlg().intValue() == 1) {
            return StringUtils.EMPTY;
        } else if (juchuu.getShouhizeikubun() == ECNames.TAX_ZEIKOMI) {
            return juchuumeisai.getZeikomiTeika().toString();
        } else {
            return juchuumeisai.getTeika()
                    .setScale(0, BigDecimal.ROUND_HALF_UP).toString();
        }
    }

    /**
     * Get value for fied saibaikeitai.
     * @param shouhin
     * @return Saibaikeitai
     */
    private String getSaibaikeitai(Shouhin shouhin) {
        List<Shouhinshoukai> shouhinshoukaiList = shouhin.getShoukai();
        for (Shouhinshoukai shouhinshoukai : shouhinshoukaiList) {
            if (shouhinshoukai.getPk().getInd() == 28L) {
                return StringUtil.nullToKara(shouhinshoukai.getNaiyou());
            }
        }
        return StringUtils.EMPTY;
    }

    /**
     * Get value for fied shouhinbunrui.
     *
     * @param shouhin
     * @return shouhinbunrui
     */
    private String getShouhinbunrui(Shouhin shouhin) {
        Shouhinbunrui shouhinbunrui = shouhin.getBunrui();
        if (shouhinbunrui != null) {
            return StringUtil.nullToKara(shouhinbunrui.getShouhinbunrui());
        }
        return StringUtils.EMPTY;
    }

    /**
     * Get value for fied value for fied shouhinshurui.
     *
     * @param shouhin
     * @return shouhinshurui
     */
    private String getShouhinshurui(Shouhin shouhin) {
        Shouhinshurui shouhinshurui = shouhin.getShurui();
        if (shouhinshurui != null) {
            return StringUtil.nullToKara(shouhinshurui.getShouhinshurui());
        }
        return StringUtils.EMPTY;
    }

    /**
     * Get value for fied cool kbn.
     *
     * @param juchuumeisai
     * @return cool hyouji
     */
    private String getCoolhyouji(Shouhin shouhin) {
        if (shouhin == null) {
            return StringUtils.EMPTY;
        }
        int coolKbn = shouhin.getCoolkubun().intValue();
        if (coolKbn == Shouhin.COOL_KUBUN_COOL) {
            return "クール";
        } else if (coolKbn == Shouhin.COOL_KUBUN_REITOU) {
            return "冷凍";
        } else {
            return StringUtils.EMPTY;
        }
    }

    /**
     * Get value for fied oikuragentei flag.
     *
     * @param shouhin
     * @return OikuragenteiFlg
     */
    private String getOikuragenteiFlg(Shouhin shouhin) {
        if (shouhin.getShouhinkubun().intValue() == 3) {
            return "1";
        }
        return "0";
    }

    /**
     * Get value for fied reitou flag.
     * @param shouhin
     * @return ReitouFlg
     */
    private String getReitouFlg(Shouhin shouhin) {
        if (shouhin == null) {
            return StringUtils.EMPTY;
        }
        int coolKbn = shouhin.getCoolkubun().intValue();
        if (coolKbn == Shouhin.COOL_KUBUN_REITOU) {
            return "1";
        }
        return "0";
    }
}
